﻿using PRO.DbProvider;
using PRO.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using PRO.ORM;
using PRO.Common;

namespace PRO.Data.SqlServer
{
    public partial class DataProvider : IDataProvider
    {
        public DataTable GetClass()
        {
            ClassInfo info = new ClassInfo();
            SqlHelper helper = new SqlSelect();
            helper.AddEntity(info);
            helper.OrderBy = "order by sort asc";
            return (DataTable)helper.Run();
        }
        public bool AddClass(ClassInfo info)
        {
            return ((int)info.Insert("id") > 0) ? true : false;
        }
        public bool DelClass(int id,out string msg)
        {
            bool r = false;
            msg = "";
            string sql = "SELECT count(id) FROM Pro_Class where pid=" + id;
            if (DbHelper.ExecuteScalar(CommandType.Text, sql).ToString().ToInt(0) == 0)
            {
                sql = "SELECT count(id) FROM Pro_Product where cid=" + id;
                if (DbHelper.ExecuteScalar(CommandType.Text, sql).ToString().ToInt(0) == 0)
                {
                    sql = "delete Pro_Class where id=" + id;
                    r = Convert.ToBoolean(DbHelper.ExecuteNonQuery(CommandType.Text, sql));
                }
                else
                    msg = "删除失败，该分类下存在产品，请先删除分类下的产品！";
            }
            else
                msg = "删除失败，该分类下存在子分类，请先删除分类下子分类！";
            return r;
        }
        public DataTable GetClassInfo(int id)
        {
            SqlHelper helper = new SqlSelect("Pro_Class");
            helper.AddFeild("id", "pid", "classname", "classdes", "sort");
            helper.AddWhereAnd("id", id, SqlDbType.Int, 4, "=");
            return (DataTable)helper.Run();
        }
        public bool EditClass(ClassInfo info)
        {
            return info.Update("sort", "pid");
        }
    }
}
